package com.schooleducation.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.schooleducation.entity.ExamDicType;
import com.schooleducation.entity.ExamDictinoary;
import com.schooleducation.entity.Exams;
import com.schooleducation.entity.LessonSubscribe;
import com.schooleducation.entity.User;
import com.schooleducation.service.ExamsService;

public class ExamsServiceImpl extends SuperService implements ExamsService {

	@Override
	public List<ExamDictinoary> getExamDictinoary(ExamDictinoary examDictinoary,int page) {
		return getDaoFac().getExamDictinoaryDao().getExamDictinoary( examDictinoary, page, 30);
	}
	
	@Override
	public List<ExamDicType> getExamDicTypes() {
		List<ExamDicType> dicTypes=getDaoFac().getExamDicTypeDao().findAll(ExamDicType.class);
		return dicTypes;
	}

	@Override
	public Exams addExam(Exams exams) {
		exams.setCreateDate(new Date());
		getDaoFac().getExamsDao().save(exams);
		return exams;
	}
	@Override
	public List<ExamDictinoary> getTestPaper(Exams exams){
		return getDaoFac().getExamDictinoaryDao().getExamDictByExam(exams.getData());
	}
	@Override
	public Exams updateExam() {
		return null;
	}

	@Override
	public void deleteExam() {
	}

	@Override
	public Exams getExam(int examid) {
		return getDaoFac().getExamsDao().findById(Exams.class, examid);
	}

	@Override
	public List<Exams> getUserExams(User user) {
		List<Exams> exams=new ArrayList<>();
		LessonSubscribe lessonSubscribe = new LessonSubscribe();
		lessonSubscribe.setUser(user);
		List<LessonSubscribe> lessonSubscribes=getDaoFac()
				.getLessonSubscribeDao()
				.getSubscribeLessonByUser(lessonSubscribe, 0, 20);
		//此做法不妥，，
		for (LessonSubscribe lessonSubscribe2 : lessonSubscribes) {
			Exams exam=lessonSubscribe2.getLesson().getExams();
			if (exam!=null&&lessonSubscribe2.getScore()==null) {
				exams.add(exam);
			}
		}
		return exams;
	}

	@Override
	public void addScore(User user, String score, String examid) {
		LessonSubscribe lessonSubscribe = new LessonSubscribe();
		lessonSubscribe.setUser(user);
		List<LessonSubscribe> lessonSubscribes=getDaoFac()
				.getLessonSubscribeDao()
				.getSubscribeLessonByUser(lessonSubscribe, 0, 20);
		for (LessonSubscribe lessonSubscribe2 : lessonSubscribes) {
			Exams exam=lessonSubscribe2.getLesson().getExams();
			if (exam!=null) {
				if (examid.equals(exam.getId().toString())) {
					lessonSubscribe2.setScore(score);
					break;
				}
			}
		}
		
	}

}
